package com.codeshellme.bookmanager.service.impl;

import com.codeshellme.bookmanager.dao.BookStockDao;
import com.codeshellme.bookmanager.dto.BookStock;
import com.codeshellme.bookmanager.service.BookStockService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BookStockServiceImpl implements BookStockService {

    @Autowired
    private BookStockDao bookStockDao;

    @Override
    public PageInfo<Map<String, Object>> queryAllBookStocks(String name, Integer type_id, Integer num_start, Integer num_end,
                                                            Integer author_id, Byte status, String date_start, String date_end,
                                                            String q,
                                                            Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> books = bookStockDao.queryAllBookStocks(
                name, type_id, num_start, num_end, author_id, status, date_start, date_end, q);
        return new PageInfo<>(books);
    }

    @Override
    public Boolean verifyBookStockId(String bookName) {
        Map<String, Object> book = bookStockDao.findBookStockByName(bookName);
        if (book == null) {
            return true;    // 没有找到书名，可用
        } else {
            return false;   // 找到了书名，不可用
        }
    }

    @Override
    public int addBookStock(BookStock stock) {
        return bookStockDao.addBookStock(stock);
    }

    @Override
    public int updateBookStockNum(Integer id, Integer num, String op) {
        if(op.equals("SUB")) {
            num = - num;
        }

        bookStockDao.updateBookStockNum(id, num);

        return 1;
    }

    @Override
    public int deleteBookStockById(Integer id) {
        return bookStockDao.deleteBookStockById(id);
    }

}
